package com.itheima.controller;


import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 上传文件
 */
@Controller("UploadController")
public class UploadController {

    @RequestMapping(value = "/test")
    public String test(HttpServletRequest request)
    {
        ServletContext servletContext = request.getServletContext();
        String contextPath = servletContext.getContextPath();
        System.out.println("contextPath: " + contextPath);
        String realPath = servletContext.getRealPath("");
        System.out.println("realPath: " + realPath);

        /**
         * contextPath: /spring_mvc_war_exploded
         * realPath: D:\yanweifeng\java\www\spring_mvc\target\spring_mvc\
         */

        return "success";
    }


    /**
     * 使用 Commons-fileupload 组件实现文件上传，需要导入该组件相应的支撑 jar 包：Commons-fileupload 和
     * commons-io。commons-io 不属于文件上传组件的开发 jar 文件，但Commons-fileupload 组件从 1.1 版本开始，它
     * 工作时需要 commons-io 包的支持
     */

    @RequestMapping(value = "/uploadFile")
    public String uploadFile(@RequestParam(value = "file", required = true) MultipartFile uploadFile, HttpServletRequest request) throws Exception {
        String fileName = "";
        String name = "";

        System.out.println("begin。。。。。");
        // 获取原始文件名称
        System.out.println("uploadFile对象：" + uploadFile);
        String uploadFileName = uploadFile.getOriginalFilename();
        System.out.println("原始文件名称：" + uploadFileName);
        // 获取文件后缀名
        String ext = uploadFileName.substring(uploadFileName.lastIndexOf(".") + 1, uploadFileName.length());
        System.out.println("文件后缀名：" + ext);
        // 文件随机数
        String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        System.out.println("随机数：" + uuid);
        // 判断是否输入了文件名
        fileName = uuid + "_" + uploadFileName;
        if (!StringUtils.isEmpty(name)) {
            fileName = uuid + "_" + name + "." + ext;
        }
        System.out.println("文件名：" + fileName);

        // 获取文件路径
        // javax.servlet-api-3.1.0.jar 才支持 getServletContext() 方法
        ServletContext servletContext = request.getServletContext();
        String realPath = servletContext.getRealPath("/uploads");
        // 时间文件夹分割
        String datePath = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        // 判断文件夹是否存在
        File file = new File(realPath + "/" + datePath);
        if (!file.exists()) {
            file.mkdirs();
        }
        System.out.println("file:" + file);
        // 移动文件到指定目录
        uploadFile.transferTo(new File(file, fileName));
        return "success";
    }
}
